// This file contains cart-related API functions
import type { 
  UpdateCartItemData, UpdateCartItemResponses,
  SelectCartItemData, SelectCartItemResponses,
  AddToCartData, AddToCartResponses,
  GetSelectedItemsData, GetSelectedItemsResponses,
  GetCartListData, GetCartListResponses,
  GetCartItemCountData, GetCartItemCountResponses,
  RemoveFromCartData, RemoveFromCartResponses,
  ClearCartData, ClearCartResponses
} from './cartTypes';
import { client as _heyApiClient } from '../../api/client.gen';
import type { Options } from '../common/apiTypes';

/**
 * 更新购物车商品
 * 更新购物车中指定商品的数量
 */
export const updateCartItem = <ThrowOnError extends boolean = false>(options: Options<UpdateCartItemData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).put<UpdateCartItemResponses, unknown, ThrowOnError>({
        url: '/api/cart/update',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

/**
 * 选择/取消选择购物车商品
 * 设置购物车商品的选择状态，用于批量结算
 */
export const selectCartItem = <ThrowOnError extends boolean = false>(options: Options<SelectCartItemData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).put<SelectCartItemResponses, unknown, ThrowOnError>({
        url: '/api/cart/select/{cartItemId}',
        ...options
    });
};

/**
 * 添加商品到购物车
 * 将指定课程添加到用户购物车
 */
export const addToCart = <ThrowOnError extends boolean = false>(options: Options<AddToCartData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).post<AddToCartResponses, unknown, ThrowOnError>({
        url: '/api/cart/add',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

/**
 * 获取选中的购物车商品
 * 获取当前用户购物车中已选中的商品列表，用于结算
 */
export const getSelectedItems = <ThrowOnError extends boolean = false>(options?: Options<GetSelectedItemsData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).get<GetSelectedItemsResponses, unknown, ThrowOnError>({
        url: '/api/cart/selected',
        ...options
    });
};

/**
 * 查询用户购物车列表
 * 获取当前登录用户的购物车商品列表
 */
export const getCartList = <ThrowOnError extends boolean = false>(options?: Options<GetCartListData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).get<GetCartListResponses, unknown, ThrowOnError>({
        url: '/api/cart/list',
        ...options
    });
};

/**
 * 获取购物车商品总数
 * 获取当前用户购物车中的商品总数量
 */
export const getCartItemCount = <ThrowOnError extends boolean = false>(options?: Options<GetCartItemCountData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).get<GetCartItemCountResponses, unknown, ThrowOnError>({
        url: '/api/cart/count',
        ...options
    });
};

/**
 * 删除购物车商品
 * 从购物车中移除指定课程
 */
export const removeFromCart = <ThrowOnError extends boolean = false>(options: Options<RemoveFromCartData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).delete<RemoveFromCartResponses, unknown, ThrowOnError>({
        url: '/api/cart/remove/{courseId}',
        ...options
    });
};

/**
 * 清空购物车
 * 清空当前用户的整个购物车
 */
export const clearCart = <ThrowOnError extends boolean = false>(options?: Options<ClearCartData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).delete<ClearCartResponses, unknown, ThrowOnError>({
        url: '/api/cart/clear',
        ...options
    });
};